import 'package:trollee/app/core/constants/storage.dart';
import 'package:trollee/app/core/utils/storage_util.dart';

class HttpProxyUtil {
  // 是否启用代理
  static bool _isEnableProxy = false;

  /// 代理服务地址，ip:port
  /// 例如：'192.168.1.105:8999';
  static String _proxyServer = '';

  static void init() {
    _isEnableProxy = StorageUtil.getBool(StorageConstants.configProxyEnable) ?? false;
    _proxyServer = StorageUtil.getString(StorageConstants.configProxyServer) ?? '';
  }

  static bool get isEnableProxy => _isEnableProxy && _proxyServer.isNotEmpty == true;

  static String get proxyServer => _proxyServer;

  static void enableProxy(String proxyServer) async {
    await StorageUtil.setBool(StorageConstants.configProxyEnable, true);
    await StorageUtil.setString(StorageConstants.configProxyServer, proxyServer);
    init();
  }

  static void clear() async {
    _isEnableProxy = false;
    await StorageUtil.remove(StorageConstants.configProxyEnable);
    init();
  }
}
